from django.shortcuts import render
from drf_haystack.viewsets import HaystackViewSet
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter

from .models import SKU
from .serializers import SKUSerializer, SKUIndexSerializer


# Create your views here.
class SKUListView(ListAPIView):
    """商品列表"""

    # 指定序列化器
    serializer_class = SKUSerializer

    # queryset = SKU.objects.filter(is_launched=True, category_id=115)

    # 指定过滤后端为排序过滤
    filter_backends = [OrderingFilter]
    # 指定以那些字段进行排序
    ordering_fields = ['create_time', 'price', 'sales']



    # 指定查询集
    def get_queryset(self):
        category_id = self.kwargs.get('category_id')  # 获取url路径中 正则组起别名提取的参数
        # self.request.query_params.get('category_id')
        return SKU.objects.filter(is_launched=True, category_id=category_id)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer
